package com.onenine.app.utils;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.onenine.common.util.Logger;

/**
 * 高德定位
 */
public class GDLocation {

    private AMapLocationClient mapLocationClient;
    private LocationCallback mCallback;

    public interface LocationCallback {
        void onResult(AMapLocation aMapLocation);
    }

    private AMapLocationListener mapLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    //定位成功
                    Logger.e("AmapLocationSuccess", aMapLocation);
                    if (mCallback != null) {
                        mCallback.onResult(aMapLocation);
                    }
                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    Logger.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                    if (mCallback != null) {
                        mCallback.onResult(null);
                    }
                }
            }
        }
    };

    /**
     * 默认定位方式
     *
     * @param context
     */
    public GDLocation(Context context) {
        initLocation(context, false, null);
    }

    /**
     * 单次定位,无论成功与否
     *
     * @param context
     * @param onceLocation
     */
    public GDLocation(Context context, boolean onceLocation) {
        initLocation(context, onceLocation, null);
    }

    /**
     * 定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
     *
     * @param context
     * @param purpose
     */
    public GDLocation(Context context, AMapLocationClientOption.AMapLocationPurpose purpose) {
        initLocation(context, true, purpose);
    }

    /**
     * 初始化定位配置
     *
     * @param context
     * @param onceLocation
     */
    private void initLocation(Context context, boolean onceLocation, AMapLocationClientOption.AMapLocationPurpose purpose) {
        mapLocationClient = new AMapLocationClient(context);
        mapLocationClient.setLocationListener(mapLocationListener);
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setNeedAddress(true);
        if (purpose != null) {
            option.setLocationPurpose(purpose);
        }
        if (onceLocation) {
            option.setOnceLocation(true);
            option.setOnceLocationLatest(true);
        }
        mapLocationClient.setLocationOption(option);
    }

    /**
     * 启动定位
     */
    public void startLocation(LocationCallback callback) {
        mCallback = callback;
        mapLocationClient.startLocation();
    }

    /**
     * 停止定位
     */
    public void stopLocation() {
        mapLocationClient.stopLocation();
    }

    /**
     * 销毁定位
     */
    public void destroyLocation() {
        mapLocationClient.onDestroy();
    }

}
